(function($, getQrCode) {
	var user_id = localStorage.getItem("telMember");//获取
	/**
	 * 创建订单
	 * */
	getQrCode.setOrder = function(state,arry){
		mui.ajax({
			headers: {
				"device-udid":headers.device_udid,
			    "device-client":headers.device_client,
			    "device-code":headers.device_code,
			    "api-version":headers.api_version,
			},
			type:'POST',
			url: publicUrl +"/sites/api/?url=admin/flow/done",
			dataType:"json",
			data:{
				"token":state.token,
				"rec_id":arry.toString(),
				"pay_id":"11",
				"shipping_id":"2",
			},
			success:function(data){
				//console.log(JSON.stringify(data));
				if(data.status.succeed == 1){
					getQrCode.getQrCode(data);
				}
			}
		})
	}
	/**
	 * 获取支付二维码
	 **/
	getQrCode.getQrCode = function(data) {
		var amount = parseInt(data.data.order_info.order_amount*100);
		var amount1 = parseFloat(data.data.order_info.order_amount);
		//console.log(amount);
		mui.ajax({
			type:'get',
			url: boss + "/NewBossWebService.asmx/GetQrCode",
			dataType:"json",
			data:{
				"amount":amount,
				"requestType":"0", 
			},
			success:function(data){
				if(data.errCode == "SUCCESS"){
					//console.log(JSON.stringify(data.billNo));
					localStorage.setItem("billQRCode",data.billQRCode);
					localStorage.setItem("responseTimestamp",data.responseTimestamp);
					localStorage.setItem("billQRCode",data.billQRCode);
					localStorage.setItem("amount",amount1.toFixed(2));
					mui.openWindow({
                        url: "pay.html",
                        id: "pay"
                    });
                    var timesRun = 0;
					var interval = setInterval(function(){
						timesRun += 1;
						if(timesRun === 20){
							clearInterval(interval);
							alert("33333");
						}
						getQrCode.GetOrderStatus(data.billNo);
					}, 10000);
				}else{
					mui(".mui-popover").popover("toggle");
					window.setTimeout(function(){
	           		 	mui.toast(data.status.error_desc,{ duration:'long', type:'div'});
	                },500);	
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
               	mui(".mui-popover").popover("toggle");
               	window.setTimeout(function(){
           		 	mui.toast(textStatus,{ duration:'long', type:'div' });
                },500);
                // 错误信息   
                console.log(textStatus);
            }
		})
	} 
	/***
	 * 查询支付结果
	 * ***/
	getQrCode.GetOrderStatus = function(billno){
		mui.ajax({
			type:'get',
			url: boss + "/NewBossWebService.asmx/GetOrderStatus",
			dataType:"json",
			data:{
				"billno":billno,
				"requestType":"0", 
			},
			success:function(data){
				//console.log(data.errCode);
				//console.log(data.billPayment.status);
				if(data.errCode == "SUCCESS" && data.billPayment.status == "TRADE_SUCCESS"){
					mui.openWindow({
                        url: "paymentSuccess.html",
                        id: "paymentSuccess"
                    });
                    return false;
				}else{
	           		//mui.toast(data.status.error_desc,{ duration:'long', type:'div'});
				}
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {
           		mui.toast(textStatus,{ duration:'long', type:'div' });
            }
		})
	}
	/**
	 * 添加商品
	 **/
	getQrCode.addgoods = function(id,num,state){
		console.log(JSON.stringify(state));
		var t=[];
		mui.ajax({
			headers: {
				"device-udid":headers.device_udid,
			    "device-client":headers.device_client,
			    "device-code":headers.device_code,
			    "api-version":headers.api_version,
			},
			type:'POST',
			url: publicUrl +"/sites/api/?url=admin/flow/checkOrder",
			dataType:"json",
			cache: false,
        	async: false,
			data:{
				"token":state.token,
				"addgoods":{
					"goods_sn":id,
            		"number":num
				},
				"user":{
                   "user_id":user_id,
                }
			},
			success:function(data){
				t = data;
			},
			error: function (XMLHttpRequest, textStatus, errorThrown) {   
                t = textStatus;
            }
		})
		return t;
	}
	/**
	 * 修改商品
	 **/
	getQrCode.updategoods = function(rec_id,num,state){
		var t=[];
		mui.ajax({
			headers: {
				"device-udid":headers.device_udid,
			    "device-client":headers.device_client,
			    "device-code":headers.device_code,
			    "api-version":headers.api_version,
			},
			type:'POST',
			url: publicUrl +"/sites/api/?url=admin/flow/checkOrder",
			dataType:"json",
			cache: false,
        	async: false,
			data:{
				"token":state.token,
				"updategoods":{
					"rec_id":rec_id,
            		"number":num,
				},
				"user":{
                   "user_id":user_id,
                }
			},
			success:function(data){
				t = data;
			}
		})
		return t;
	}
	/**
	 * 删除商品
	 **/
	getQrCode.deletegoods = function(rec_id,state){
		var t=[];
		mui.ajax({
			headers: {
				"device-udid":headers.device_udid,
			    "device-client":headers.device_client,
			    "device-code":headers.device_code,
			    "api-version":headers.api_version,
			},
			type:'POST',
			url: publicUrl +"/sites/api/?url=admin/flow/checkOrder",
			dataType:"json",
			cache: false,
        	async: false,
			data:{
				"token":state.token,
				"deletegoods":{
					"rec_id":rec_id,
				},
				"user":{
                   "user_id":user_id,
                }
			},
			success:function(data){
				t = data;
			}
		})
		return t;
	}
	/**
	 * 更新页面信息
	 **/
	getQrCode.updateHtml =function(goods_list){
		var goods_sn =[];
		for(var i=0; i<goods_list.length; i++){
			goods_sn.push(goods_list[i].goods_sn);
			jQuery("#" +goods_list[i].goods_sn + "_rec_id").val(goods_list[i].rec_id);
			jQuery("#" +goods_list[i].goods_sn + "_goods_price").val(goods_list[i].goods_price);
		}
		getQrCode.getShopping(goods_sn);
	}
	/**
	 * 计算购物车内容
	 **/
	getQrCode.getShopping = function(goods_sn){
		var sum = 0;
		var num = 0; 
		for(var i=0; i<goods_sn.length; i++){
			if(document.getElementById(goods_sn[i] + "_checkbox").checked){
				var inputVal = jQuery("#" + goods_sn[i].replace(/\"/g, "") + "_input").val();
				num += parseInt(inputVal);
				sum += parseInt(inputVal) * parseFloat(jQuery("#" + goods_sn[i] + "_goods_price").val())
			}
		}
		jQuery("#my_goods_num").html(num);
		jQuery("#my_goods_num_two").html(num);
		jQuery("#my_sum").html(sum.toFixed(2));
		jQuery("#my_sum_Two").html(sum.toFixed(2));
	}
}(mui, window.getQrCode = {}));